package com.park.web.service.impl;

import com.park.web.base.baseOperation.BaseService;
import com.park.web.dao.pbaOwnerInfoMapper;
import com.park.web.dao.pbaOwnerVehicleInfoMapper;
import com.park.web.model.domain.pbaOwnerInfo;
import com.park.web.model.vo.OwnerVehicleInfoVo;
import com.park.web.service.pbaOwnerInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Service
public class pbaOwnerInfoServiceImpl extends BaseService<pbaOwnerInfo> implements pbaOwnerInfoService {

    @Autowired
    pbaOwnerInfoMapper ipbaOwnerInfoMapper;
    @Autowired
    pbaOwnerVehicleInfoMapper ipbaOwnerVehicleInfoMapper;


    public void saveOwnerInfo(OwnerVehicleInfoVo entity) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        var pbaOwnerInfo = entity.getOwnerInfo();
        pbaOwnerInfo.setOwnerCode("CS" + simpleDateFormat.format(new Date()));
        pbaOwnerInfo.setOrganizationCode(entity.getOrganization());
        pbaOwnerInfo.setPermissionCodes(StringUtils.join(entity.getPermissionGroup(), ","));
        pbaOwnerInfo.setBalance(0.0);
        pbaOwnerInfo.setDeposit(0.0);
        pbaOwnerInfo.setCreateTime(new Date());
        ipbaOwnerInfoMapper.insert(entity.getOwnerInfo());
        entity.getVehicleArray().forEach(x -> {
            x.setId(UUID.randomUUID().toString().replace("-", ""));
            x.setOwnerCode(pbaOwnerInfo.getOwnerCode());
            x.setCreateTime(new Date());
            ipbaOwnerVehicleInfoMapper.insert(x);
        });
    }
}